package cn.com.clearbus.result;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

@ApiModel(value="统一响应对象",description="统一响应对象,三个元素：code、msg、data")
public class DynamicResult<T> {

    @ApiModelProperty(value = "状态码", example = "200")
    private String code;
    @ApiModelProperty(value = "响应消息", example = "成功")
    private String msg;
    @ApiModelProperty(value = "数据")
    private T data;

    public DynamicResult() {
        this(ErrCode.SUCCESS, null);
    }

    public DynamicResult(ErrCode errCodeApp) {
        this(errCodeApp, null);
    }

    public DynamicResult(T result) {
        this(ErrCode.SUCCESS, result);
    }

    public DynamicResult(ErrCode errCodeApp, T data) {
        if (errCodeApp == null) {
            this.code = ErrCode.SUCCESS.getCode();
            this.msg = ErrCode.SUCCESS.getMsg();
        } else {
            this.code = errCodeApp.getCode();
            this.msg = errCodeApp.getMsg();
        }
        this.data = data;
    }

    //临时方法，慎用
    public DynamicResult(String code, String msg, T data) {
        if (StringUtils.isBlank(code)) {
            this.code = ErrCode.SUCCESS.getCode();
        } else {
            this.code = code;
        }
        if (StringUtils.isBlank(msg)) {
            this.msg = ErrCode.SUCCESS.getMsg();
        } else {
            this.msg = msg;
        }
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}

